const cheerio = require('cheerio');
const URI = require('urijs');

function parsingGovContents($contents, $, url) {
    let contents = [];
    $contents.each(function() {
        let text = $(this).text();
        contents.push(parsingGovContent($(this), url));
    });
    return contents;
}


function parsingGovContent($content, url) {
    let content = {};
    // content.date = new Date($content.find('td').eq(1).text().trim());
    // var contentArr = $content.find('td').eq(0).text().split(' ');
    // content.index = parseInt(contentArr[0]) || 0;
    // content.title = contentArr[1].trim();
    // content.link = $content.find('a').attr('href');

    let $linkTag = $content.find('a').eq(0);
    if (!$linkTag.length) {
        return {
            title: '',
            link: '',
            date: null
        }
    }
    let title = $linkTag.attr('title') || $linkTag.text();
    let link = $linkTag.attr('href');
    if (link.indexOf('.') === 0) {
        link = URI(url + link).normalizePathname().toString();
    } else if (link.indexOf('/') === 0) {
        link = URI(url).origin().toString() + link;
    }

    let dateRegResult = /\d{4}-\d{1,2}-\d{1,2}/.exec($content.text());
    let date;
    if (dateRegResult) {
        date = dateRegResult[0];
    }
    content = {
        title,
        link,
        date
    }
    return content;
}

function removeHTML(text) {
    let $ = cheerio.load(text);
    let ret = $.text();
    return ret;
}

function parsingWeixinContents(weixinList) {
    if (!Array.isArray(weixinList)) {
        return [];
    }
    let contents = [];
    for (const weixinItem of weixinList) {
        contents = contents.concat(parsingWeixinContent(weixinItem));
    }
    return contents;

}

function parsingWeixinContent(weixinItem) {
    let result = [];
    let title = weixinItem.app_msg_ext_info.title;
    let link = 'https://mp.weixin.qq.com' + weixinItem.app_msg_ext_info.content_url;
    let date = new Date(parseInt(weixinItem.comm_msg_info.datetime) * 1000).toLocaleString();
    let content = {
        title,
        link,
        date,
        weixin: weixinItem.app_msg_ext_info
    }
    result = [content];
    if (Array.isArray(weixinItem.app_msg_ext_info.multi_app_msg_item_list)) {
        let subContents = weixinItem.app_msg_ext_info.multi_app_msg_item_list.map(parsingWeixinSubContent);
        for (const subContent of subContents) {
            subContent.date = date;
        }
        result = [content].concat(subContents);
    }
    return result;
}

function parsingWeixinSubContent(weixinData) {
    let title = weixinData.title;
    let link = 'https://mp.weixin.qq.com' + weixinData.content_url;
    let weixin = weixinData;
    return {
        title,
        link,
        weixin
    }
}


function filterContent(origin, words, type) {
    return origin.filter(item => {
        for (let word of words) {
            word = word.toLowerCase();
            if (item.title.toLowerCase().indexOf(word) >= 0) {
                return true;
            }
            if (type === 'weibo') {
                if (item.weibo.text.toLowerCase().indexOf(word) >= 0) {
                    return true;
                }
                if (item.weibo.retweeted_status) {
                    if (item.weibo.retweeted_status.text && item.weibo.retweeted_status.text.toLowerCase().indexOf(word) >= 0) {
                        return true;
                    }
                    if (item.weibo.retweeted_status.page_info && item.weibo.retweeted_status.page_info.content1.toLowerCase().indexOf(word) >= 0) {
                        return true;
                    }
                }
            } else if (type === 'weixin') {
                if (item.weixin.digest.indexOf(word) >= 0) {
                    return true;
                }
            }
        }
        return false;
    })
}

module.exports = {
    parsingGovContents,
    removeHTML,
    parsingWeixinContents,
    filterContent
}